package com.lhz.collector.controller;

import com.lhz.collector.common.RestfulResult;
import com.lhz.collector.entity.Collector;
import com.lhz.collector.service.CollectorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * Created by LHZ on 2017/4/1.
 */
@RestController
@RequestMapping("/api")
public class ColllectorController {
    @Autowired
    private CollectorService collectorService;
    //添加采集器
    @RequestMapping(value = "/collectors",method = RequestMethod.POST)
    public ResponseEntity add(@RequestBody Collector collector){
        RestfulResult restfulResult=new RestfulResult();
        if(collectorService.add(collector)){
            restfulResult.setStatusCode(RestfulResult.STATUS_CODE_SUCCESS);
            restfulResult.setMessage("添加成功");
        }else{
            restfulResult.setMessage("添加失败");
        }
        return ResponseEntity.ok(restfulResult);
    }
    //查询所有采集器
    @RequestMapping(value = "/collectors")
    public ResponseEntity getAll(){
        return ResponseEntity.ok(collectorService.findAll());
    }

    //查询单个采集器
    @RequestMapping(value = "/collectors/{id}")
    public ResponseEntity getById(@PathVariable int id){
        return ResponseEntity.ok(collectorService.findById(id));
    }

    //修改采集器
    @RequestMapping(value = "/collectors",method = RequestMethod.PUT)
    public ResponseEntity update(@RequestBody Collector collector){
        RestfulResult restfulResult=new RestfulResult();
        if(collectorService.update(collector)){
            restfulResult.setStatusCode(RestfulResult.STATUS_CODE_SUCCESS);
            restfulResult.setMessage("修改成功");
        }else{
            restfulResult.setMessage("修改失败");
        }
        return ResponseEntity.ok(restfulResult);
    }

    //删除采集器
    @RequestMapping(value = "/collectors/{id}",method = RequestMethod.DELETE)
    public ResponseEntity delete(@PathVariable long id){
        RestfulResult restfulResult=new RestfulResult();
        if(collectorService.delete(id)){
            restfulResult.setStatusCode(RestfulResult.STATUS_CODE_SUCCESS);
            restfulResult.setMessage("删除成功");
        }else{
            restfulResult.setMessage("删除失败");
        }
        return ResponseEntity.ok(restfulResult);
    }
}
